package com.easylinkin.linkappapi.asset.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.asset.entity.AssetRefManager;
import com.easylinkin.linkappapi.asset.mapper.AssetRefManagerMapper;
import com.easylinkin.linkappapi.asset.service.AssetRefManagerService;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 设备资产关联用户 服务实现类
 * </p>
 *
 * @author XuGuoQing
 * @since 2020-04-15
 */
@Service
public class AssetRefManagerServiceImpl extends ServiceImpl<AssetRefManagerMapper, AssetRefManager> implements AssetRefManagerService {

    @Override
    public int batchDelete(List<String> assetId) {
        QueryWrapper<AssetRefManager> wrapper = new QueryWrapper();
        wrapper.in("asset_id", assetId);
        return baseMapper.delete(wrapper);
    }

    @Override
    public boolean batchAdd(String assetId, List<String> managerIdList) {
        if (managerIdList == null || managerIdList.isEmpty()) {
            return true;
        }
        managerIdList = managerIdList.stream().distinct().collect(Collectors.toList());
        Assert.isTrue(isManagerIdAllExist(managerIdList), "管理人员不能是不存在的");
        managerIdList.forEach(managerId -> {
            this.save(new AssetRefManager(assetId, managerId));
        });
        return true;
    }

    @Override
    public boolean isAssetRefManagers(String assetId, List<String> managerIdList) {
        managerIdList = managerIdList.stream().distinct().collect(Collectors.toList());
        // 这里要对ManagerIdList进行校验,暂时没有 先默认都存在
        Map<String, Object> columnMap = new HashMap<String, Object>();
        columnMap.put("asset_id", assetId);
        List<AssetRefManager> assetRefManagerList = baseMapper.selectByMap(columnMap);
        List<String> assetIdList = assetRefManagerList.stream().map(AssetRefManager::getUserId).collect(Collectors.toList());
        if (managerIdList.size() == assetRefManagerList.size() && managerIdList.containsAll(assetIdList)) {
            return true;
        }
        return false;
    }

    @Override
    public boolean isManagerIdAllExist(List<String> managerIdList) {
        // 这里要对ManagerIdList进行校验,暂时没有 先默认都存在

        return true;
    }


}
